package com.shwread.android.ui.widget;

import android.app.Dialog;
import android.content.Context;
import android.support.v4.view.ViewPager.LayoutParams;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;
import com.shwread.android.qysw10000136.R;
import org.geometerplus.fbreader.book.Bookmark;

public class BookMarkDialog
        extends
        Dialog
        implements
        android.view.View.OnClickListener
{
	private TextView                    deleteBookMark;
	private Context                     context;
	
	private OnBookMarkMenuClickListener bookMarkMenuClickListener;
	private String                      deleteContent;
	
	/**
	 * @return the deleteContent
	 */
	public String getDeleteContent() {
		return deleteContent;
	}

	/**
	 * @param deleteContent the deleteContent to set
	 */
	public void setDeleteContent(String deleteContent) {
		this.deleteContent = deleteContent;
		deleteBookMark.setText(deleteContent);
	}

	public BookMarkDialog(Context context, OnBookMarkMenuClickListener bookMarkMenuClickListener) {
		super(context, R.style.common_dialog);
		this.bookMarkMenuClickListener = bookMarkMenuClickListener;
		this.context = context;
		setCanceledOnTouchOutside(true);
		findViews();
		setListeners();
		getWindow().setLayout(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
		getWindow().setGravity(Gravity.BOTTOM);
		getWindow().setWindowAnimations(android.R.style.Animation_InputMethod);
	}
	
	private void findViews() {
		View view = LayoutInflater.from(context).inflate(R.layout.book_mark_menu, null);
		deleteBookMark = (TextView) view.findViewById(R.id.bookmark_menu_delete);
		setContentView(view);
	}
	
	private void setListeners() {
		deleteBookMark.setOnClickListener(this);
	}
	
	public void setBookMark(Bookmark bookmark) {
		deleteBookMark.setTag(bookmark);
	}
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_MENU)
		{
			this.dismiss();
		}
		return super.onKeyDown(keyCode, event);
	}
	
	@Override
	public void onClick(View v) {
		this.dismiss();
		switch (v.getId()) {
			case R.id.bookmark_menu_delete:
				Bookmark bookmark = (Bookmark) v.getTag();
				if (bookmark.getMyTypeID() == 1) {
					bookMarkMenuClickListener.deleteBookMark(bookmark);
				} else {
					bookMarkMenuClickListener.deleteBookNote(bookmark);
				}
				break;
		}
	}
	
	public interface OnBookMarkMenuClickListener
	{
		void deleteBookMark(Bookmark bookmark);
		
		void deleteBookNote(Bookmark bookmark);
	}
}
